package com.example.cart;

import static android.view.View.VISIBLE;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.cart.adapter.CartAdapter;
import com.example.cart.dto.Cart;
import com.example.cart.dto.CartWithGoods;
import com.example.cart.repository.CartDao;

import java.util.Collections;
import java.util.List;

public class CartActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView cartCountView;
    private CartDao cartDao;
    private ListView cartTable;
    private View toShopping;
    private CartAdapter cartAdapter;
    private TextView totalPriceView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_cart);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        cartDao = CartApplication.get().getGoodsDataBase().cartDao();

        cartTable = findViewById(R.id.cartTable);

        findViewById(R.id.goBack).setOnClickListener(view->{
            finish();
        });
        cartCountView = findViewById(R.id.cartCount);

        toShopping = findViewById(R.id.toShopping);


        toShopping.setOnClickListener(view->{
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            startActivity(intent);
        });

        cartAdapter = new CartAdapter(this, Collections.emptyList(), this);

        cartTable.setAdapter(cartAdapter);

        totalPriceView = findViewById(R.id.totalPrice);

        loadCarts();




    }

    private void loadCarts() {
        //cartTable.removeAllViews();
        List<CartWithGoods> carts = cartDao.page(0,10);


        //int widthPixels = getResources().getDisplayMetrics().widthPixels;

        //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(widthPixels / 5, LinearLayout.LayoutParams.WRAP_CONTENT);

        // 通知 ListView 数据刷新
        cartAdapter.update(carts);

        if (carts.isEmpty()) {
            toShopping.setVisibility(VISIBLE);

        }else{
            toShopping.setVisibility(View.GONE);
        }

        double totalPrice = carts.stream().mapToDouble((cart) -> {
            return cart.getCartQuantity() * Double.parseDouble(cart.getPrice());
        }).sum();

        totalPriceView.setText("￥"+totalPrice);
        /*for (CartWithGoods cart : carts) {

            CheckBox checkBox = new CheckBox(this);

            cartTable.addView(checkBox,layoutParams);

            View.OnClickListener goGoodsDetailListener = goGoodsDetailListener(cart.getId());

            ImageView imageView = new ImageView(this);
            imageView.setImageBitmap(BitmapFactory.decodeFile(cart.getImageSrc()));
            imageView.setOnClickListener(goGoodsDetailListener);
            cartTable.addView(imageView,layoutParams);


            TextView name = new TextView(this);
            name.setText(cart.getName());
            name.setOnClickListener(goGoodsDetailListener);
            cartTable.addView(name,layoutParams);

            TextView quantity = new TextView(this);
            quantity.setText(String.valueOf(cart.getCartQuantity()));
            cartTable.addView(quantity,layoutParams);


            Button delete = new Button(this);
            delete.setText("删除");
            delete.setTag(cart.getCartId());
            delete.setOnClickListener(this);
            cartTable.addView(delete,layoutParams);

        }*/
    }

    private View.OnClickListener goGoodsDetailListener(int goodsId){
        return view -> {
            Intent intent = new Intent(this, GoodsDetailActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("goods_id",goodsId);
            startActivity(intent);
        };
    }


    @Override
    protected void onResume() {
        super.onResume();


        cartCountView.setText(String.valueOf(CartApplication.get().getCartCount()));
    }

    @Override
    public void onClick(View v) {

        int cartId = (int) v.getTag();
        new AlertDialog.Builder(this)
                .setPositiveButton("删除", (DialogInterface dialog, int which) -> {
                    Cart cart = new Cart();
                    cart.setId(cartId);
                    cartDao.delete(cart);
                    loadCarts();
                }).setNegativeButton("取消", null)
                .setTitle("确认删除此项？")
                .create().show();

        return;
    }
}